#include "myScreen.h"

MyScreen::MyScreen()
{
}

MyScreen::~MyScreen()
{
}

void MyScreen::Render()
{
	this->pEngine->GetGraphics()->DrawCamera(this->pMyCamera);

	this->pEngine->GetGraphics()->EnableDepthTesting();

	this->pEngine->GetGraphics()->DrawEntity(this->pMyEntity[0]);
	this->pEngine->GetGraphics()->DrawEntity(this->pMyEntity[1]);
	this->pEngine->GetGraphics()->DrawEntity(this->pMyEntity[2]);

	this->pEngine->GetGraphics()->DisableDepthTesting();

	this->pEngine->GetGraphics()->Setup2DProjection();
}

void MyScreen::Update(float fDelta)
{
	this->pMyEntity[0]->UpdateAnimation(fDelta);
	this->pMyEntity[1]->UpdateAnimation(fDelta);
	this->pMyEntity[2]->UpdateAnimation(fDelta);
}

void MyScreen::Load()
{
	this->pMyCamera = new Camera(0, 0, this->ScreenWidth(), this->ScreenHeight());
	this->pMyCamera->SetPerspectiveProjection(60.0f, 1.33f, 0.1f, 300.0f);
	this->pMyCamera->SetBackgroundColor(Color(255, 189, 38));
	this->pMyCamera->Move(0, 0, 10);

	this->pMyTexture = this->pEngine->GetGraphics()->LoadTexture("media/box.bmp");

	this->pMyEntity[0] = new AnimatedEntity();
	this->pMyEntity[0]->AddModel(Model::CreateCube(1, 1, 1));

	Model *pModel = this->pMyEntity[0]->GetModel(0);
	pModel->SetTexture(this->pMyTexture);

	this->pMyEntity[0]->AddPose();
	this->pMyEntity[0]->AddPose();
	this->pMyEntity[0]->AddPose();

	ModelPose *pPose;

	pPose = this->pMyEntity[0]->GetPose(0)->GetModelPose(0);
	for (int iVertexIndex = 0; iVertexIndex < pModel->GetMesh()->GetVertexCount(); iVertexIndex ++)
		pPose->SetVertexPosition(iVertexIndex, pPose->GetVertexPosition(iVertexIndex) + Vector3(1, 0, 0));

	pPose = this->pMyEntity[0]->GetPose(1)->GetModelPose(0);
	for (int iVertexIndex = 0; iVertexIndex < pModel->GetMesh()->GetVertexCount(); iVertexIndex ++)
		pPose->SetVertexPosition(iVertexIndex, pPose->GetVertexPosition(iVertexIndex) + Vector3(0, 1, 0));

	pPose = this->pMyEntity[0]->GetPose(2)->GetModelPose(0);
	for (int iVertexIndex = 0; iVertexIndex < pModel->GetMesh()->GetVertexCount(); iVertexIndex ++)
		pPose->SetVertexPosition(iVertexIndex, pPose->GetVertexPosition(iVertexIndex) + Vector3(-1, 0, 0));

	this->pMyEntity[0]->AddAnimation("move", 0, 2, 1);

	this->pMyEntity[1] = AnimatedEntity::ShallowCopy(this->pMyEntity[0]);
	this->pMyEntity[2] = AnimatedEntity::ShallowCopy(this->pMyEntity[0]);

	this->pMyEntity[0]->PlayAnimation("move", DelGEGraphics_Animation_Playstyle_Loopback);
	this->pMyEntity[1]->PlayAnimation("move", DelGEGraphics_Animation_Playstyle_Loop);
	this->pMyEntity[2]->PlayAnimation("move", DelGEGraphics_Animation_Playstyle_Once);

	this->pMyEntity[1]->Move(0, 2, 0);
	this->pMyEntity[2]->Move(0, -2, 0);
}

void MyScreen::Unload()
{
	this->pMyEntity[0]->Destroy();

	delete this->pMyCamera;
	delete this->pMyEntity[0];
	delete this->pMyEntity[1];
	delete this->pMyEntity[2];
	delete this->pMyTexture;
}